#!/bin/bash
if [ -z "$COLUMNSTORE_INSTALL_DIR" ]; then
	COLUMNSTORE_INSTALL_DIR=/usr/local/mariadb/columnstore
fi

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR

# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
strContains() {
	str="$1"
	sub="$2"
	if test "${str#*$sub}" != "$str"; then
	        return 0    # $sub is in $str
	else
		return 1    # $sub is not in $str
	fi
}

# Determine where Hadoop is installed
hadoop=`which hadoop 2>/dev/null`
if [ -z $hadoop ]; then
	echo "Hadoop package is not installed and detected by the 'which' command"
	echo "Please Install an Hadoop Package"
	return 1
fi

tmppath=$hadoop
while true; do
	path=`readlink $tmppath`
	if [ -z $path ]; then
		hadooppath=$tmppath
		break
	fi

	tmppath=$path
done

strContains "$hadooppath" "bin/hadoop"
if [ $? -eq 1 ]; then
	echo "Hadoop binary path not located in $hadooppath"
	echo "Please Contact InfiniDB Customer Support. "
	echo "Exiting"
	return 1
fi

# get hadoop base directory
basepath=$(dirname $hadooppath)
basepath=$(dirname $basepath)

# get hadoop library directory
find $basepath -name libhdfs.so.0.0.0 > /tmp/libhds.txt
libpath=`head -n1 /tmp/libhds.txt`
if [ -z $libpath ]; then
	echo "Library libhdfs.so.0.0.0 not found in $libpath"
	echo "Please Contact InfiniDB Customer Support. "
	echo "Exiting"
	return 1
fi

libpath=$(dirname $libpath)

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$libpath

#get Hadoop libexec path
find $basepath -name hadoop-config.sh > /tmp/config.txt

# if libexec is in there, go get it
libexec=`grep libexec /tmp/config.txt`
if [ ! -z $libexec ]; then
	libexecpath=`grep libexec /tmp/config.txt | head -1`
else # use the top one in the file
	libexecpath=`head -n1 /tmp/config.txt`
fi

if [ ! -f $libexecpath ]; then
	echo "hadoop-config.sh doesn't exist in $basepath"
	echo "Please Contact InfiniDB Customer Support. "
	echo "Exiting"
	return 1
fi

HADOOP_LIBEXEC_DIR=$(dirname $libexecpath)

# Detemine JAVA_HOME
find $libpath -name bigtop-detect-javahome > /tmp/bigtop.txt
bigtop=`head -n1 /tmp/bigtop.txt`
if [ ! -z $bigtop ]; then
	. $bigtop
fi

# if JAVA_HOME is set, check if its valid
if [ ! -z $JAVA_HOME ]; then
	javalibfile=`find $JAVA_HOME -name libjvm.so`
	
	find $JAVA_HOME -name libjvm.so > /tmp/java.txt
	javalibfile=`head -n1 /tmp/java.txt`
	if [ -z $javalibfile ]; then
		# JAVA_HOME is invalid, clear it
		export JAVA_HOME=
	fi
fi

# attempt to find java, if its not setup yet
if [ -z $JAVA_HOME ]; then
	for candidate in \
		/usr/lib/j2sdk1.6-sun \
		/usr/lib/jvm/java-6-sun \
		/usr/lib/jvm/java-1.6.0-sun-1.6.0.* \
		/usr/lib/jvm/java-1.6.0-sun-1.6.0.*/jre/ \
		/usr/lib/jvm/j2sdk1.6-oracle \
		/usr/lib/jvm/j2sdk1.6-oracle/jre \
		/usr/java/jdk1.6* \
		/usr/java/jre1.6* \
		/usr/java/jdk1.7* \
		/usr/java/jre1.7* \
		/usr/lib/jvm/j2sdk1.7-oracle \
		/usr/lib/jvm/j2sdk1.7-oracle/jre \
		/Library/Java/Home \
		/usr/java/default \
		/usr/lib/jvm/default-java \
		/usr/lib/jvm/java-openjdk \
		/usr/lib/jvm/jre-openjdk \
		/usr/lib/jvm/java-1.7.0-openjdk* \
		/usr/lib/jvm/java-7-openjdk* \
		/usr/lib/jvm/java-7-oracle* \
		/usr/lib/jvm/java-1.6.0-openjdk \
		/usr/lib/jvm/java-1.6.0-openjdk-* \
		/usr/lib/jvm/jre-1.6.0-openjdk* ; do
		if [ -e $candidate/bin/java ]; then
			export JAVA_HOME=$candidate
			break
		fi
	done
fi

if [ ! -d $JAVA_HOME ]; then
	echo "Java Home directory doesn't exist"
	echo "Please Contact InfiniDB Customer Support. "
	echo "Exiting"
	return 1
fi

find $JAVA_HOME -name libjvm.so > /tmp/java.txt
javalibfile=`head -n1 /tmp/java.txt`
if [ -z $javalibfile ]; then
	echo "Java Library libjvm.so doesn't exist in $JAVA_HOME"
	echo "Please Contact InfiniDB Customer Support. "
	echo "Exiting"
	return 1
fi

javalibpath=$(dirname $javalibfile)

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$javalibpath
export JAVA_HOME=$JAVA_HOME

#set in config file
$COLUMNSTORE_INSTALL_DIR/bin/setConfig -d Installation JavaHome $JAVA_HOME
$COLUMNSTORE_INSTALL_DIR/bin/setConfig -d Installation JavaPath $javalibpath

#run hadoop-config
. $HADOOP_LIBEXEC_DIR/hadoop-config.sh

# Add the jars from hadoop classpath to the CLASSPATH.
hadoop classpath | tr : \\n > /tmp/hadoopClasspath.txt
for f in `cat /tmp/hadoopClasspath.txt`; do
	strContains "$f" ".jar"
	if [ $? -eq 0 ]; then
                CLASSPATH=${CLASSPATH}:$f;
        fi
done
export CLASSPATH=$CLASSPATH

#echo "Using"
#echo "Hadoop bash path " $basepath
#echo "Hadoop library path " $libpath
#echo "Hadoop execlib path " $HADOOP_LIBEXEC_DIR
#echo "java_home " $JAVA_HOME
#echo "java_path " $javalibpath

